package com.icoolkj.ms.common.security.adapter;

import com.icoolkj.ms.common.core.feign.FeignInsideAuthConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/02/10 20:30
 */
@Component
public class DefaultAuthConfigAdapter implements AuthConfigAdapter {

    private static final Logger logger = LoggerFactory.getLogger(DefaultAuthConfigAdapter.class);

    public DefaultAuthConfigAdapter() {
        logger.info("not implement other AuthConfigAdapter, use DefaultAuthConfigAdapter all url need auth.");
    }

    /**
     * 内部直接调用接口，无需登录权限
     */
    private static final String FEIGN_INSIDER_URI = FeignInsideAuthConfig.FEIGN_INSIDE_URL_PREFIX + "/insider/**";

    /**
     * 外部直接调用接口，无需登录权限 public
     */
    private static final String EXTERNAL_URI = "/**/public/**";

    /**
     * swagger
     */
    private static final String DOC_URI = "/v3/api-docs";
    private static final String DOC_UI = "/swagger-ui/**";

    @Override
    public List<String> pathPatterns() {
        return Collections.singletonList("/**");
    }

    @Override
    public List<String> excludePathPatterns(String... paths) {
        List<String> excludePaths = new ArrayList<>();
        // 添加默认的排除路径
        addDefaultExcludePaths(excludePaths);
        // 处理传入的额外路径
        if (paths != null) {
            excludePaths.addAll(Arrays.asList(paths));
        }
        return excludePaths;
    }

    private void addDefaultExcludePaths(List<String> excludePaths) {
        excludePaths.add(DOC_URI);
        excludePaths.add(DOC_UI);
        excludePaths.add(FEIGN_INSIDER_URI);
        excludePaths.add(EXTERNAL_URI);
    }
}
